package com.yootk.config;

import com.yootk.di.config.MessageConfig;
import com.yootk.di.service.MessageService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;
import java.util.Map;

/**
 * 定义配置类
 */
//@Configuration  // 该注解也表示进行Bean注册
public class YootkConfig {

    // 如果此时定义的Bean没有编写任何的名称，则使用方法名称作为Bean名称
    @Bean
    public MessageConfig config() {
        MessageConfig config = new MessageConfig();
        config.setHost("www.yootk.com");
        config.setPort(9090);
        config.setEnable(true);
        return config;
    }

    // 自己编写一个Bean名称
    @Bean("definit")
    public MessageConfig definit() {
        MessageConfig config = new MessageConfig();
        config.setHost("www.jixianit.com");
        config.setPort(8989);
        config.setEnable(true);
        return config;
    }

    @Bean("edu")
    public MessageConfig edu() {
        MessageConfig config = new MessageConfig();
        config.setHost("edu.yootk.com");
        config.setPort(7979);
        config.setEnable(true);
        return config;
    }

    // 大部分的情况下都不需要编写名称
    @Bean
    public MessageService messageServiceMain(
                MessageConfig config,
                MessageConfig definit,
                MessageConfig edu) {
        Map<String, MessageConfig> map = new HashMap<>();
        map.put("yootk", config);
        map.put("muyan", definit);
        map.put("edu", edu);
        MessageService service = new MessageService();
        service.setConfigs(map);
        return service;
    }

    @Bean
    @Primary    // 进行主要注入的配置
    public MessageService messageServiceBack(
            MessageConfig config,
            MessageConfig definit,
            MessageConfig edu) {
        Map<String, MessageConfig> map = new HashMap<>();
        map.put("yootk", config);
        map.put("jixianit", definit);
        map.put("edu", edu);
        MessageService service = new MessageService();
        service.setConfigs(map);
        return service;
    }
}
